package com.xunxi.mimi.upload;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.functions.Function;


/**
 */
public class RxUtils {

    /** 
     * 多线程合并工具
     * 数量必须是大于0小于等于9，否则为空
     */
    public static <T> Flowable<T> RxMutilThreadMerge(ArrayList<Flowable<T>> subFuncList) {
        if (subFuncList == null) {
            return null;
        }else{
            return Flowable.merge(subFuncList);
        }
    }
    public static <T> Flowable<List<T>> RxMutilThreadZip(ArrayList<Flowable<T>> subFuncList) {
        if (subFuncList == null) {
            return null;
        }
        int count = subFuncList.size();
        if (count > 0) {

            return Flowable.zip(subFuncList, new Function<Object[], List<T>>() {
                @Override
                public List<T> apply(Object... objects) {
                    List<T> list = new ArrayList<T>();
                    for(Object obj : objects){
                        list.add((T)obj);
                    }
                    return list;
                }
            });
        }
        return null;
    }
    public static <T> Flowable<ArrayList<T>> RxMutilThreadZip(List<Flowable<T>> arrayObject) {
            if(arrayObject == null || arrayObject.size() == 0){
                 return Flowable.just(new ArrayList<T>());
            }
            return Flowable.zip(arrayObject, new Function<Object[], ArrayList<T>>() {
                @Override
                public ArrayList<T> apply(Object[] objects) throws Exception {
                    ArrayList<T> list = new ArrayList<>();
                    for(Object obj : objects){
                        list.add((T)obj);
                    }
                    return list;
                }
            });
    }
}
